package com.fast.mybatis.mapper.holder;

import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.fast.mybatis.annotation.Column;
import com.fast.mybatis.annotation.Id;
import com.fast.mybatis.annotation.OrderBy;
import com.fast.mybatis.annotation.Table;
import com.fast.mybatis.enums.OrderType;
import com.fast.mybatis.model.ColumnMeta;
import com.fast.mybatis.model.IdMeta;
import com.fast.mybatis.model.OrderByMeta;

public class PojoMeta {

	private  Table table;
	
	private List<IdMeta> primaryList = new ArrayList<>();
	
	private List<ColumnMeta> columnList = new ArrayList<>();
	
	private List<OrderByMeta> orderByList = new ArrayList<>();
	
	/**
	 * 数据库表所对应的java pojo类
	 */
	public final Class<?> pojoClz;
	
	private String tableName;

	
	private Type pojoArrayClz;
	
	private List<Field> fieldList;

	public PojoMeta(Table table,Class<?> pojoClz, List<Field> fieldList) {
		this.pojoClz = pojoClz;
		this.fieldList = fieldList;
		this.table = table;
		this.tableName = table.name();
	}
	
	public void resolve() {
		this.pojoArrayClz = Array.newInstance(this.pojoClz, 0).getClass();
		ColumnMeta columnMeta = null;
		IdMeta idMeta = null;
		OrderByMeta orderByMeta = null;
		for(Field f : fieldList) {
			Column c = f.getAnnotation(Column.class);
			if(c != null) {
				columnMeta = new ColumnMeta();
				columnMeta.setColumnName(c.columnName());
				columnMeta.setName(f.getName());
				columnMeta.setJavaType(c.javaType());
				columnMeta.setJdbcType(c.jdbcType());
				columnList.add(columnMeta);
			}
			
			Id id = f.getAnnotation(Id.class);
			if(id != null) {
				idMeta = new IdMeta();
				idMeta.setColumnMeta(columnMeta);
				primaryList.add(idMeta);
			}
			
			OrderBy orderBy = f.getAnnotation(OrderBy.class);
			if(orderBy != null) {
				orderByMeta = new OrderByMeta();
				orderByMeta.setOrder(orderBy.order());
				if(orderBy.orderBy() != null) {
					orderByMeta.setOrderBy(orderBy.orderBy());
				}else {
					orderByMeta.setOrderBy(OrderType.ASC);
				}
				orderByMeta.setColumnMeta(columnMeta);
				orderByList.add(orderByMeta);
			}
			
		}
		Collections.sort(columnList,new ColumnMetaComparator());
		
	}

	public Type pojoArrayClz() {
		return this.pojoArrayClz;
	}

	public Table getTable() {
		return table;
	}

	public void setTable(Table table) {
		this.table = table;
	}

	public List<IdMeta> getPrimaryList() {
		return primaryList;
	}

	public void setPrimaryList(List<IdMeta> primaryList) {
		this.primaryList = primaryList;
	}

	public List<ColumnMeta> getColumnList() {
		return columnList;
	}

	public void setColumnList(List<ColumnMeta> columnList) {
		this.columnList = columnList;
	}

	public List<OrderByMeta> getOrderByList() {
		return orderByList;
	}

	public void setOrderByList(List<OrderByMeta> orderByList) {
		this.orderByList = orderByList;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public Type getPojoArrayClz() {
		return pojoArrayClz;
	}

	public void setPojoArrayClz(Type pojoArrayClz) {
		this.pojoArrayClz = pojoArrayClz;
	}

	public List<Field> getFieldList() {
		return fieldList;
	}

	public void setFieldList(List<Field> fieldList) {
		this.fieldList = fieldList;
	}

	public Class<?> getPojoClz() {
		return pojoClz;
	}

	

}
